<script>
	import {
		AutoUpdate
	} from '@/utils/weixinAutoUpdate';
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex';
	import businessList from '@/common/business';
	import {
		uniLinkTo
	} from './utils/uniPromise';

	export default {
		globalData: {
			appOpen: false, //当前app是否已经打开
		},
		onLaunch: function(options) {
			this.$u.vuex('vuex_meal_list', []) //小程序首次启动先清除一次餐饮购物车数据	
			// #ifdef MP-WEIXIN
			// if(options.ShareTimeline == 1){/*小程序分享进来的*/
			// 	uni.reLaunch({
			// 		url:options.sharePath,
			// 	})
			// }
			// #endif
			this.getSystemData()
			// 更新商家信息
			this.$loading()
			this.pageInit(options)


			//  #ifdef MP-ALIPAY	
			try {
				if (options.query.baidu == 1) {
					this.$u.vuex('vuex_router_query', options.query)
				}
			} catch (err) {

			}
			// #endif


		},
		computed: {
			...mapState(['vuex_superior', 'noAuth', 'vuex_login_back_url'])
		},
		onShow: function(options) {
			if (this.globalData.appOpen) {
				this.onShowByWeixin(options);
			} else {
				this.globalData.appOpen = true;
			}
		},
		onPageNotFound: function(err) {
			uniLinkTo('/pages/index/index');
		},
		methods: {
			...mapActions(['loadBusinessInfo', 'loadUserInfo', 'checkIsUpdateSuperior', 'getSystemData',
				'loadTabBarList'
			]),
			// ...mapMutations(['checkCurrentTab']),
			pageInit(options) {
				this.loadBusinessInfo()
					.then(() => {
						if (this.vuex_business.status != 1) {
							uni.redirectTo({
								url: `/package/common/renovation/index?name=${this.vuex_business.name}`
							})
							return
						}
						// 检测当前tab激活项
						// this.checkCurrentTab();
						this.getTabBar(options)
					})
					.catch(() => {
						setTimeout(() => {
							this.pageInit(options)
						}, 300)
					});
			},
			getTabBar(options) {
				// 加载底部栏
				this.loadTabBarList()
					.then(() => {
						// #ifndef H5
						this.weixinHandle(options);
						// #endif
						// #ifdef H5
						this.onLaunchByH5(options);
						// #endif
					}).catch(() => {
						setTimeout(() => {
							this.getTabBar(options)
						}, 300)
					})
			},
			// 更新用户信息
			updateUser() {
				if (this.$isLogin()) {
					this.loadUserInfo().then(() => {
						this.$hide()
						this.$isResolve();
					}).catch(() => {
						this.$hide()
						this.$isResolve();
					})
				} else {
					this.$hide()
					this.$isResolve();
				}
			},
			// #ifndef H5
			// 非H5页面检查分享参数和加载用信息
			weixinHandle(options) {
				// 检测分销商id
				this.onShowByWeixin(options);
				this.updateUser()
				// 小程序检测更新
				AutoUpdate();
			},
			// 检测分销商id
			onShowByWeixin(options) {
				// 检测是否更换上级
				// 支付宝没有如果路径没传参数options.query不存在 其他小程序如微信会给个空的对象，支付宝直接没有
				let superior = undefined
				if (options.query && options.query.superior) {
					superior = options.query.superior
				}
				this.checkIsUpdateSuperior(superior);
			},
			// #endif
			// #ifdef H5
			// h5页面首次进入
			onLaunchByH5() {
				let bol = this.noAuth.some((item) => {
					return item === this.$route.path
				})
				if (!bol && !this.$isLogin() && this.$route.path !== '/package/common/auth/index') {
					this.$u.vuex('vuex_login_back_url', this.$route.fullPath);
					this.$hide()
					this.$isResolve();
					this.$linkTo('/package/common/auth/index', 'redirectTo');
					return
				}
				this.H5Handle()
			},
			H5Handle() {
				let query = this.$route.query || {};
				if (query.superior) {
					// 检测是否更换上级
					this.checkIsUpdateSuperior(query.superior);
				}
				this.updateUser()
			},
			// #endif
		}
	};
</script>
<style lang="scss">
	/*每个页面公共css */
	/*引入uView基础样式*/
	@import 'uview-ui/index.scss';
	@import 'static/css/common.scss';
	@import 'static/css/iconfont.css';
</style>
